import time

from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait


def get_chrome_html_driver(url=None,port=9222):
    dcap = dict(DesiredCapabilities.CHROME)
    # dcap["executablePath"] = 'D:/chromedriver-win64/chromedriver.exe'
    chrome_options = Options()
    # chrome_options.add_argument('--headless')
    # chrome_options.add_argument('--proxy-server=http://127.0.0.1:10011')
    # chrome_options.add_argument("--remote-debugging-port=9222")
    # chrome_options.add_argument("--user-data-dir=C:/selenum/AutomationProfile1")
    chrome_options.add_argument("--start-maximized")
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:{0}".format(port))
    chrome_options.add_argument(
        '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
    )
    chrome_options.add_argument('--ignore-certificate-errors')
    print("连接浏览器")
    driver = webdriver.Chrome(chrome_options=chrome_options, desired_capabilities=dcap)
    print("浏览器连接完成")
    # driver.set_window_size(1920, 1080)
    if url is not None:
        driver.get(url)
    return driver

def get_headless_chrome_html_driver(url=None,port=9222):
    dcap = dict(DesiredCapabilities.CHROME)
    # dcap["executablePath"] = 'D:/chromedriver-win64/chromedriver.exe'
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--proxy-server=http://127.0.0.1:10011')
    # chrome_options.add_argument("--remote-debugging-port=9222")
    # chrome_options.add_argument("--user-data-dir=C:/selenum/AutomationProfile1")
    # 实测无效
    # chrome_options.add_argument("--disable-images")
    # chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:{0}".format(port))
    chrome_options.add_argument(
        '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
    )
    chrome_options.add_argument('--ignore-certificate-errors')
    print("连接浏览器")
    driver = webdriver.Chrome(chrome_options=chrome_options, desired_capabilities=dcap)
    print("浏览器连接完成")
    driver.set_window_size(1920, 1080)
    if url is not None:
        driver.get(url)
    return driver

def method_find_element_by_class(driver,class_name):
    return driver.find_element_by_class_name(class_name)

def wait(driver,method,timeout=10):
    wait = WebDriverWait(driver, timeout, 1);
    wait.until(method,"wait fail!")


def test():
    driver = webdriver.Chrome()
    driver.get("https://www.okx.com/zh-hans/web3/marketplace/nft/collection/eth/cryptopunks")
    time.sleep(5)

# test()
# time.sleep(5)

